package sky.cin.framework.network.dto;

import sky.cin.framework.network.http.Method;
import sky.cin.framework.network.http.Response;

import java.util.Arrays;
import java.util.stream.Collectors;

public class HttpServletResponse extends Response {
    
    public HttpServletResponse(Response response) {
        super(response);
    }

    public HttpServletResponse(String data) {
        super(data);
    }

    public HttpServletResponse() {
    }

    public Response getWriter() {
        return getResponse();
    }

    public void NotFound404(String path) {
        setStatus(404);
        getWriter().write("<h1>404 Not Found</h1>\npath: " + path);
    }


    public void setAllowCredentials(boolean b) {
        setHeader("access-control-allow-credentials", b);
    }

    public void setAllowHeaders(String[] headers) {
        String str = Arrays.toString(headers);
        str = str.substring(1, str.length() - 1).replace(", ", ",");
        setHeader("access-control-allow-headers", str);
    }

    public void setAllowMethods(Method[] methods) {
        String str = Arrays.stream(methods).map(method -> method.name() + ",").collect(Collectors.joining());
        str = str.substring(0, str.length() - 1);
        setHeader("access-control-allow-methods", str);
    }

    public void setAllowOrigin(String origin) {
        setHeader("access-control-allow-origin", origin);
    }

    public String toString() {
        return super.toString();
    }

}
